package com.niit.yhserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.niit.yhpojo.dto.SalesOrderDTO;
import com.niit.yhpojo.entity.SalesOrder;
import com.niit.yhserver.mapper.SalesOrderMapper;
import com.niit.yhserver.service.SalesOrderService;
import com.niit.yhserver.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SalesOrderServiceImpl implements SalesOrderService {

        @Autowired
        private SalesOrderMapper salesOrderMapper;

        //显示全部
        @Override
        public List<SalesOrder> selectAll() {
                return salesOrderMapper.selectAll();
        }

        @Override
        public Result queryOrderList(String orderNum, String userName, String userPhone, String status, Integer pageNum, Integer pageSize) {
                PageHelper.startPage(pageNum, pageSize);
                List<SalesOrder> orders = salesOrderMapper.selectByCondition(orderNum, userName, userPhone, status);
                PageInfo<SalesOrder> pageInfo = new PageInfo<>(orders);
                return Result.success(pageInfo);
        }

        @Override
        public Result getOrderById(Long id) {
                SalesOrder order = salesOrderMapper.selectById(id);
                if (order == null) {
                        return Result.fail("订单不存在");
                }
                return Result.success(order);
        }

        @Override
        public Result createOrder(SalesOrderDTO orderDTO) {
                // 字段验证
                if (orderDTO.getOrderNum() == null || orderDTO.getOrderNum().trim().isEmpty()) {
                        return Result.fail("订单号不能为空");
                }
                if (orderDTO.getUserId() == null) {
                        return Result.fail("用户ID不能为空");
                }
                if (orderDTO.getUserName() == null || orderDTO.getUserName().trim().isEmpty()) {
                        return Result.fail("用户姓名不能为空");
                }
                if (orderDTO.getProductName() == null || orderDTO.getProductName().trim().isEmpty()) {
                        return Result.fail("商品名称不能为空");
                }
                if (orderDTO.getProductId() == null) {
                        return Result.fail("商品ID不能为空");
                }
                if (orderDTO.getQuantity() == null || orderDTO.getQuantity() <= 0) {
                        return Result.fail("数量必须大于0");
                }
                if (orderDTO.getTotalPrice() == null || orderDTO.getTotalPrice() <= 0) {
                        return Result.fail("总价必须大于0");
                }
                // 设置创建时间和更新时间
                orderDTO.setCreatedAt(new Date());
                orderDTO.setUpdatedAt(new Date());
                //保存订单
                SalesOrder order = new SalesOrder();
                BeanUtils.copyProperties(orderDTO, order);
                order.setUserId(orderDTO.getUserId());
                order.setUserName(orderDTO.getUserName());
                order.setUserPhone(orderDTO.getUserPhone());
                order.setProductName(orderDTO.getProductName());
                order.setProductId(orderDTO.getProductId());
                order.setCreatedAt(new Date());
                order.setUpdatedAt(new Date());
                salesOrderMapper.insert(order);
                return Result.success(order);
        }

        @Override
        public Result updateOrder(Long id, SalesOrderDTO orderDTO) {
                // 字段验证
                if (orderDTO.getOrderNum() == null || orderDTO.getOrderNum().trim().isEmpty()) {
                        return Result.fail("订单号不能为空");
                }

                SalesOrder existingOrder = salesOrderMapper.selectById(id);
                if (existingOrder == null) {
                        return Result.fail("订单不存在");
                }

                // 只更新必要的字段，避免覆盖不想修改的字段
                existingOrder.setOrderNum(orderDTO.getOrderNum());
                existingOrder.setUserId(orderDTO.getUserId());
                existingOrder.setUserName(orderDTO.getUserName());
                existingOrder.setUserPhone(orderDTO.getUserPhone());
                existingOrder.setProductName(orderDTO.getProductName());
                existingOrder.setProductId(orderDTO.getProductId());
                existingOrder.setQuantity(orderDTO.getQuantity());
                existingOrder.setTotalPrice(orderDTO.getTotalPrice());
                existingOrder.setStatus(orderDTO.getStatus());
                existingOrder.setUpdatedAt(new Date());

                salesOrderMapper.update(existingOrder);
                return Result.success(existingOrder);
        }

        @Override
        public Result deleteOrder(Long id) {
                SalesOrder order = salesOrderMapper.selectById(id);
                if (order == null) {
                        return Result.fail("订单不存在");
                }
                salesOrderMapper.deleteById(id);
                return Result.success("删除成功");
        }
}